package com.cocotingo.snail.samples;

import com.cocotingo.snail.dispatcher.DispatchQueue;
import com.cocotingo.snail.dispatcher.Dispatcher;

public class SampleMain3 {
	
	/**
	 * Simulates a long-time task with specified time and finish-message.
	 */
	static class SampleLongTask implements Runnable {
		int delay;
		String msg;
		public SampleLongTask(int delay, String msg) {
			this.delay = delay;
			this.msg = msg;
		}
		@Override
		public void run() {
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(msg);
		}
	}

	public static void main(String[] args) {
		
		// starts up 12 threads at same time:
		/*
		Dispatcher.execute(new SampleLongTask(600, "Task 1 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 2 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 3 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 4 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 5 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 6 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 7 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 8 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 9 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 10 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 11 done."));
		Dispatcher.execute(new SampleLongTask(600, "Task 12 done."));
		*/
		
		// executes 5 tasks one by one in another thread (dispatch queue):
		DispatchQueue q = new DispatchQueue(4);
		q.execute(new SampleLongTask(600, "Task 1 done."));
		q.execute(new SampleLongTask(600, "Task 2 done."));
		q.execute(new SampleLongTask(600, "Task 3 done."));
		q.execute(new SampleLongTask(600, "Task 4 done."));
		q.execute(new SampleLongTask(600, "Task 5 done."));
		q.execute(new SampleLongTask(600, "Task 6 done."));
		q.execute(new SampleLongTask(600, "Task 7 done."));
		q.execute(new SampleLongTask(600, "Task 8 done."));
		q.execute(new SampleLongTask(600, "Task 9 done."));
		q.execute(new SampleLongTask(600, "Task 10 done."));
		q.execute(new SampleLongTask(600, "Task 11 done."));
		q.execute(new SampleLongTask(600, "Task 12 done."));
		
		q.execute(new Runnable() {
			@Override
			public void run() {
				Dispatcher.drainThreadPool();
			}
		});
		
		System.out.println("main done.");
	}

}
